<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="父级目录" prop="parentPath">
				<el-input disabled v-model="dataForm.parentPath" placeholder=""></el-input>
			</el-form-item>
			<el-form-item label="选择数据库" prop="databaseId">
				<el-select
					class="choosedataLayerId"
					:disabled="dataForm.stateMenu == 'newAdd' ? false : true"
					v-model="dataForm.databaseId"
					placeholder="选择数据库"
				>
					<el-option v-for="item in dataForm.databaseList" :key="item.value" :label="item.label" :value="item.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="dataForm.name" placeholder="名称"></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="type" v-if="dataForm.parentId != 0">
				<el-select v-model="dataForm.type" placeholder="类型" :disabled="!!dataForm.id">
					<el-option :key="0" label="目录" :value="0" />
					<el-option :key="1" label="主数据" :value="1" />
				</el-select>
			</el-form-item>
			<el-form-item label="序号" prop="orderNo">
				<el-input-number v-model="dataForm.orderNo" :min="1" :max="9999" placeholder="序号"></el-input-number>
			</el-form-item>
			<el-form-item label="描述" prop="description">
				<el-input type="textarea" v-model="dataForm.description"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" :loading="loading" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useMasterDataCatalogApi, useMasterDataCatalogSubmitApi } from '@/api/data-governance/masterDataCatalog'
import { layerList } from '@/api/auth-hm'

const emit = defineEmits(['refreshDataList'])
// const props = defineProps(['childProp'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
	parentId: '',
	parentPath: '',
	path: '',
	name: '',
	type: 0,
	orderNo: 0,
	description: '',
	databaseId: '',
	databaseList: [],
	stateMenu: ''
})

const loading = ref(false)
// watch(
// 	props,
// 	newValue => {
// 		if (newValue.childProp) {
// 			dataForm.databaseId = newValue.childProp
// 		} else {
// 			dataForm.databaseId = ''
// 		}
// 	},
// 	{ immediate: true, deep: true }
// )
const init = (id?: number, parentId: any, parentPath: any, num: any, name: any) => {
	dataForm.stateMenu = name
	visible.value = true
	dataForm.id = ''
	layerListFun()
	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}
	dataForm.parentId = parentId
	dataForm.parentPath = parentPath
	dataForm.databaseId = num
	if (id) {
		getCatalog(id)
	}
}
const layerListFun = () => {
	layerList({}).then((res: any) => {
		if (res.code == 0) {
			let data = res.data
			for (let i = 0; i < data.length; i++) {
				data[i].value = data[i].id
				data[i].label = data[i].cnName
			}
			dataForm.databaseList = data
		} else {
			ElMessage.error(res.msg)
		}
	})
}
const getCatalog = (id: number) => {
	useMasterDataCatalogApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	databaseId: [{ required: true, message: '请选择数据库', trigger: 'change' }],
	type: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	orderNo: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}
		dataForm.type = dataForm.parentId == 0 ? 0 : dataForm.type
		dataForm.dataLayerId = dataForm.databaseId
		loading.value = true
		useMasterDataCatalogSubmitApi(dataForm)
			.then(() => {
				ElMessage.success({
					message: '操作成功',
					duration: 500,
					onClose: () => {
						loading.value = false
						visible.value = false
						emit('refreshDataList')
					}
				})
			})
			.catch(() => {
				loading.value = false
			})
	})
}

defineExpose({
	init
})
</script>
<style lang="scss" scoped>
.choosedataLayerId {
	margin-bottom: 0;
}
</style>